package com.coco.group.service.impl;

import com.coco.common.result.ResultData;
import com.coco.group.mapper.GroupBuyGoodsMapper;
import com.coco.group.mapper.GroupBuyItemMapper;
import com.coco.group.pojo.TbGroupBuyGoods;
import com.coco.group.pojo.TbGroupBuyItem;
import com.coco.group.service.GroupBuyGoodsService;
import com.coco.group.vo.GroupGoodsAndItemVo;
import com.coco.group.vo.GroupGoodsSearchVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Package: com.coco.group.service.impl
 * @Author: 张dy
 * @Date: Created in 2021/7/29 下午 8:00
 */
@Service
public class GroupBuyGoodsServiceImpl implements GroupBuyGoodsService {

    @Autowired
    private GroupBuyGoodsMapper groupBuyGoodsMapper;
    @Autowired
    private GroupBuyItemMapper groupBuyItemMapper;

    @Override
    public void addGroupGoods(GroupGoodsAndItemVo groupGoodsAndItemVo) {
        //spu
        TbGroupBuyGoods spu = groupGoodsAndItemVo.getGroupBuyGoods();
        //sku
        List<TbGroupBuyItem> skuList = groupGoodsAndItemVo.getGroupBuyItemList();
        //设置spu限量
        spu.setSumLimit(skuList.stream().mapToInt(TbGroupBuyItem::getLimitNum).sum());
        //设置spu限量剩余
        spu.setLimitResidue(skuList.stream().mapToInt(TbGroupBuyItem::getLimitResidue).sum());
        //添加spu拼团商品
        groupBuyGoodsMapper.addGroupGoods(spu);
        //获取spu自增id
        Long spuId = spu.getId();
        //添加sku 设置sku的spuId
        skuList.forEach(item->{
            item.setGoodsId(spuId);
            groupBuyItemMapper.add(item);
        });


    }

    @Override
    public List<TbGroupBuyGoods> findAll() {
        return groupBuyGoodsMapper.findAll();
    }

    @Override
    public ResultData<TbGroupBuyGoods> search(GroupGoodsSearchVo groupGoodsSearchVo) {
        PageHelper.startPage(groupGoodsSearchVo.getPage(),groupGoodsSearchVo.getLimit());
        List<TbGroupBuyGoods> goods=groupBuyGoodsMapper.search(groupGoodsSearchVo);
        PageInfo<TbGroupBuyGoods> tbGroupBuyGoodsPageInfo = new PageInfo<>(goods);
        return new ResultData<TbGroupBuyGoods>(tbGroupBuyGoodsPageInfo.getList(), tbGroupBuyGoodsPageInfo.getTotal());
    }

    @Override
    public void updateStatus(Long id, String status) {
        groupBuyGoodsMapper.updateStatus(id,status);
    }

    @Override
    public void deleteGroupGoods(Long id) {
        groupBuyGoodsMapper.deleteGroupGoods(id);
    }

    @Override
    public void updateGroupGoods(GroupGoodsAndItemVo goods) {
        //新的spu信息
        TbGroupBuyGoods groupBuyGoods = goods.getGroupBuyGoods();
        //原来的 spu 和 sku
        GroupGoodsAndItemVo one = findOne(groupBuyGoods.getId());

        //新的sku信息
        List<TbGroupBuyItem> groupBuyItemList = goods.getGroupBuyItemList();

        //删除原来的sku
        one.groupBuyItemList.forEach(groupBuyItem -> {
            if (groupBuyItemList.size() > 0) {
                groupBuyItemMapper.deleteStatus(groupBuyItem.getId());
            }
        });
        //把新的sku添加一下
        groupBuyItemList.forEach(groupBuyItem -> {
            groupBuyItem.setGoodsId(one.groupBuyGoods.getId());
            groupBuyItemMapper.add(groupBuyItem);
        });
        groupBuyGoodsMapper.updateGroupGoods(groupBuyGoods);
    }

    @Override
    public void addStock(Long gid, Integer num) {
        groupBuyGoodsMapper.addStock(gid,num);
    }

    @Override
    public TbGroupBuyGoods findGoodsById(Long id) {

        return groupBuyGoodsMapper.findGoodsById(id);
    }

    /**
     * 修改spu的参与人数和限量剩余
     * @param gid
     */
    @Override
    public void updateJoinNumAndStock(Long gid) {
        groupBuyGoodsMapper.updateJoinNumAndStock(gid);
    }

    @Override
    public GroupGoodsAndItemVo findOne(Long id) {
        //spu
        TbGroupBuyGoods spu = groupBuyGoodsMapper.findOne(id);
        //sku
        List<TbGroupBuyItem> skuList = groupBuyItemMapper.findByGoodsId(id);
        return new GroupGoodsAndItemVo(spu,skuList);
    }
}
